/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyDatabaseHandler;

import java.util.ArrayList;
//import MyDatabase.MyStatementExecution.*;
//import MyDatabase.MyCondition.*;
import java.util.Scanner;
import MyDataContainer.*;
import MyStatementExecution.ExecutionHandler;
/**
 *
 * @author Welcome
 */
public class DatabaseHandler {
    
    //<editor-fold defaultstate="collapsed" desc="1/Cac thuoc tinh co ban">
    private String databaseName;
    private ArrayList<MyTable> tables;
    private XMLHandler xmlHandle;
    private String pathFile;
    private ExecutionHandler executionHandler;
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="2/Cac phuong thuc khoi tao">
    public DatabaseHandler()
    {
        databaseName="";
        tables = new ArrayList<MyTable>();
        xmlHandle=new XMLHandler();
        pathFile="";
        executionHandler = new ExecutionHandler(this);
    }
    public DatabaseHandler(String _pathFile) {
        this.pathFile=_pathFile;
        tables=new ArrayList<MyTable>();
        xmlHandle = new XMLHandler(pathFile);
        databaseName="";
        executionHandler = new ExecutionHandler(this);
    }

    public DatabaseHandler(ArrayList<MyTable> tables) {
        this.databaseName = "";
        this.tables = tables;
        this.xmlHandle = null;
        this.pathFile = "";
        executionHandler = new ExecutionHandler(this);
    }
    
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="3/Cac phuong thuc Getter/Setter">
    
    public String getDatabaseName() {
        return databaseName;
    }
    
    public void setDatabaseName(String databaseName) {
        this.databaseName = databaseName;
    }
    
    public void setPathFile(String pathFile) {
        this.pathFile = pathFile;
    }
    
    public void setTables(ArrayList<MyTable> tables) {
        this.tables = tables;
    }
    
    public String getPathFile() {
        return pathFile;
    }
    
    
    public ArrayList<MyTable> getTables() {
        return tables;
    }
    
    public MyTable getTable(String tableName)
    {
        tableName.toLowerCase();
        for(int i=0; i<tables.size();i++)
        {
            String tempString = tables.get(i).getName().toLowerCase();
            if(tempString.equals(tableName))
                return new MyTable(tables.get(i));
        }
        return new MyTable();
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="4/Cac phuong thuc xu ly nghiep vu">
    
    /**
     * Đọc database từ file xml vào bộ nhớ, đường dẫn lưu sẵn trong pathFile
     */
    public void ReadDatabase()
    {
        //xmlHandle.loadDatabaseFromXMLFile(pathFile,this);
        if(pathFile.equals(""))
        {
            System.out.println("No such file exist!");
            return;
        }
        databaseName = xmlHandle.getDatabaseName();
        xmlHandle.SetDataIntoTable(tables);
    }
    
    /**
     * Đọc database từ file xml vào bộ nhớ, theo đường dẫn _pathFile
     * @Param _pathFile
     */
    public void ReadDatabase(String _pathFile)
    {
        this.pathFile=_pathFile;
        xmlHandle.setPathFile(pathFile);
        ReadDatabase();
    }
    
    /**
     * Viết database hiện tại từ bộ nhớ vào file xml, đường dẫn desPathFile
     * @Param String desPathFile
     */
    public void WriteDatabase(String desPathFile)
    {
        xmlHandle.writeDatabaseToXMLFile(desPathFile,this);
        
    }
    
    public void writeTable(String desPathFile,MyTable myTable)
    {
        //MyTable temp=(MyTable)myQuery;
        xmlHandle.writeTableToXMLFile(desPathFile, myTable);
    }
    
    /**
     * Dùng để test
     */
    public void run()
    {
        //he he, you know what I am doing here :D
        
        String end="";
        
        
        
        while (!end.toLowerCase().equals("yes"))
        {
        
            System.out.println("Nhap câu truy van: ");
            Scanner scan = new Scanner(System.in);
            String sqlString = scan.nextLine();
            
            executionHandler.processExecution(sqlString);
            System.out.println("Ket thuc: ");
            end=scan.nextLine();
        }
         
         
    }
    
    /**
     * Lấy số lượng table có trong database, trả về kiểu String
     * @return 
     */
    public String getNumberTablesByString() 
    {
        String result = String.valueOf(tables.size());
        return result;
    }
    
    
 
    //</editor-fold>
    
    
}
